package com.hs.pre.module.bardian;

import java.util.List;
import java.util.Map;

import com.hs.HsBase;
import com.hs.core.db.DBOperator;
import com.hs.core.db.DatabaseConnection;
import com.hs.pre.model.ProfileBean;
import com.hs.pre.model.SortBean;
import com.hs.pre.page.PageParam;
import com.hs.pre.page.PageQuery;
import com.hs.pre.page.PageUtil;
import com.hs.pre.page.ReverseLoadData;



public class BardianDao {

	public PageUtil  getProfileList(PageParam pageParam )
	{
		String sql = (String) HsBase.getAnsiSqlMap().get("pre_profile.select");
		try {
			PageQuery a= new PageQuery(sql,null,pageParam,new ReverseLoadData(){
				public Object load(Map map)
				{
					ProfileBean profileBean = new ProfileBean();
					profileBean.setConfigStr((String)map.get("configStr"));
					profileBean.setId((Long)map.get("id"));
					profileBean.setIsUse((Integer)map.get("isUse"));
					profileBean.setLoginId((String)map.get("loginId"));
					profileBean.setViewNum((Integer)map.get("viewNum"));
					return profileBean;
				}
			});
			return a.getPageUtil();
		} catch (Exception e) { 
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 添加个性设置
	 * @param profileBean
	 * @return
	 */
	public ProfileBean addProfile(ProfileBean profileBean)
	{	
		DBOperator dbo = null;
		try {
			dbo = DatabaseConnection.getDBOperator();
			if (dbo == null)
				return profileBean;
			this.upNotUser(profileBean.getLoginId());
			String sql = (String) HsBase.getAnsiSqlMap().get("pre_profile.insert");;
			dbo.update(sql, new Object[]{profileBean.getLoginId(),profileBean.getConfigStr(),profileBean.getIsUse(),profileBean.getViewNum()});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return profileBean;
	}
	
	/**
	 * 删除个性设置
	 * @param profileBean
	 * @return
	 * @throws Exception 
	 */
	public void delProfile(Long profileId) throws Exception
	{	
		DBOperator dbo = null;
			dbo = DatabaseConnection.getDBOperator();
		String sql = (String) HsBase.getAnsiSqlMap().get("pre_profile.delete");;
		dbo.update(sql, new Object[]{profileId});
	}
	
	/**
	 * 更新个性设置
	 * @param profileBean
	 * @return
	 * @throws Exception 
	 */
	public void updateProfile(Long profileId,String userId) throws Exception
	{	
		DBOperator dbo = null;
			dbo = DatabaseConnection.getDBOperator();
		this.upNotUser(userId);
		String sql = (String) HsBase.getAnsiSqlMap().get("pre_profile.update");;
			dbo.update(sql, new Object[]{profileId});
	}
	
	private void upNotUser(String userId) throws Exception
	{
		DBOperator dbo = null;
		dbo = DatabaseConnection.getDBOperator();
		String sql = (String) HsBase.getAnsiSqlMap().get("pre_profile.updateU");;
		dbo.update(sql, new Object[]{userId});
	}
	
	
	/***
	 * 活动改用户正在使用的个性设置
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	public ProfileBean getBardianDao(String userId) throws Exception
	{
		String sql = (String) HsBase.getAnsiSqlMap().get("pre_profile.selectuser");;
		DBOperator dbo = null;
		dbo = DatabaseConnection.getDBOperator();
		List list = dbo.select(sql, new Object[]{userId});
		if(list.isEmpty())
			return null;
		Map map = (Map)list.get(0);
		ProfileBean profileBean = new ProfileBean();
		profileBean.setConfigStr((String)map.get("configStr"));
		profileBean.setId((Long)map.get("id"));
		profileBean.setIsUse((Integer)map.get("isUse"));
		profileBean.setLoginId((String)map.get("loginId"));
		profileBean.setViewNum((Integer)map.get("viewNum"));
		return profileBean;
	}
}
